package com.zonski.jbots.game.entity;

import com.zonski.jbots.engine.Entity;
import com.zonski.jbots.engine.Engine;
import com.zonski.jbots.engine.UsableResource;
import com.zonski.jbots.engine.QuickVector;
import com.zonski.jbots.engine.entity.MovingEntity;
import com.zonski.jbots.engine.entity.Mind;
import com.zonski.jbots.game.JBotsConstants;

import java.util.Vector;

/**
 * Created by IntelliJ IDEA.
 * User: Chris Glover
 * Date: Oct 20, 2003
 * Time: 6:44:46 PM
 * To change this template use Options | File Templates.
 */
public class TankBotMind extends SeekerBotMind
{
    private int oldDirection;
    private int shootCount;
    private int baddies;
    private boolean blocked;
    private int maxShootCount = 60;

    public TankBotMind()
    {
        this.createCharge = true;
    }

    public void update(Engine engine)
        throws Exception
    {
        boolean foundBaddies = (this.baddies & (0x01 << this.getEntity().direction)) > 0;
        this.shootCount --;
        if(this.getEntity().direction != this.oldDirection && foundBaddies && shootCount < 0)
        {
            // start blasting
            UsableResource resource = (UsableResource)this.getEntity().getResource(JBotsConstants.GUN);
            resource.startUsing(this.getEntity().direction);
            this.shootCount = maxShootCount;
        }
        this.oldDirection = this.getEntity().direction;
        super.update(engine);
        if(this.getEntity().direction == this.oldDirection &&
                ((MovingEntity)this.getEntity()).getTargetDirection() == this.oldDirection)
        {
            UsableResource comet = (UsableResource)this.getEntity().getResource(JBotsConstants.COMET);
            if(this.baddies != 0 && shootCount < 0)
            {
                // fire off a comet
                comet.startUsing(this.getEntity().direction);
            }else{
                comet.stopUsing();
            }
            if(shootCount <= 0)
            {
                // stop blasting
                UsableResource resource = (UsableResource)this.getEntity().getResource(JBotsConstants.GUN);
                resource.stopUsing();
                this.baddies = 0;
            }
        }
    }


    public int getDesirability(QuickVector vector, int distance, int direction)
    {
        int desirability;
        if(distance == 1)
        {
            this.blocked = false;
            if(direction == this.getEntity().direction)
            {
                desirability = 50;
            }else{
                desirability = 40 + this.random.nextInt() % 13;
            }
        }else{
            desirability = 0;
        }
        if(vector != null)
        {
            for(int i=vector.size(); i > 0; )
            {
                i--;
                Entity entity = (Entity)vector.elementAt(i);
                if((entity.entityType.getId() == JBotsConstants.BOMB_ID ||
                        entity.entityType.getId() == JBotsConstants.EXPLOSION_ID) &&
                        distance < 3)
                {
                    desirability -= 3 - distance;
                }else if(entity.layerId != this.getEntity().layerId){
                    if(JBotsConstants.isBot(entity.entityType.getId()))
                    {
                        desirability ++;
                        if(!blocked)
                        {
                            this.baddies = this.baddies | (0x01 << direction);
                        }
                    }else if(JBotsConstants.isObstacle(entity.entityType.getId())){
                        this.blocked = true;
                    }
                }else{
                    this.blocked = true;
                }
            }
        }
        return desirability;
    }

    public Mind copy()
    {
        return new TankBotMind();
    }
}
